import axios from 'axios';
import { ElMessage } from 'element-plus'

const request = axios.create({
    baseURL: 'http://localhost:5173/api/',
    timeout: 3000
})

request.interceptors.request.use(config => {
    // 在发送请求之前做些什么
    // config.headers.token='abcdefj'
    return config;
}, error => {

    return Promise.reject(error);
})

request.interceptors.response.use(response => {
    if (response.status !== 200) {
        return Promise.reject(new Error('请求失败'));
    }
    return response.data;
}, error => {
    let msg=''
    let status=error.response.status
    if(status===401){
        msg='请先登录'
    }else if(status===403){
        msg='权限不足'
    }else{
        msg='请求失败'
    }
    ElMessage.error(msg)
    return Promise.reject(error);
})

export default request;